using System.Collections.Generic;
using System.Linq;
using Managistics.Core.Infrastructure;
using Managistics.Core.Repositories;
using Managistics.Core.Services;
using Managistics.Model;

namespace Managistics.Core.Services.Impl
{
    public class GoodService : IGoodService
    {
        private readonly IGoodRepository _goodRepository;

        public GoodService(IGoodRepository goodRepository)
        {
            _goodRepository = goodRepository;
        }
        
        [UnitOfWork]
        public List<Good> GetGoodList()
        {
            /* Used UnitOfWork attribute, because GetAll method returns IQueryable<Good>, it does not fetches records from database. 
             * No database operation is performed until ToList(). Thus, we need to control connection open/close in this method using UnitOfWork. */
            return _goodRepository.GetAll().OrderBy(good => good.Name).ToList();
        }

        public void CreateGood(Good good)
        {
            /* Not used UnitOfWork attribute, because this method only calls one repository method and repoository can manage it's connection/transaction. */
            _goodRepository.Insert(good);
        }

        public void UpdateGood(Good good)
        {
            /* Not used UnitOfWork attribute, because this method only calls one repository method and repoository can manage it's connection/transaction. */
            _goodRepository.Update(good);
        }

        [UnitOfWork]
        public void DeleteGood(string goodId)
        {
            /* Used UnitOfWork attribute, because we use different repositories and all operations must be transactional. */
            _goodRepository.Delete(goodId);
        }
    }
}